<?php
// +----------------------------------------------------------------------
// | Description: 客户模块设置
// +----------------------------------------------------------------------
// | Author: Michael_xu | gengxiaoxu@5kcrm.com 
// +----------------------------------------------------------------------

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Customer;
use addons\qingdongams\model\OperationLog;
use addons\qingdongams\model\Staff;
use addons\qingdongams\model\Message;
use think\Db;
use think\Exception;

/**
 * 客户模块设置
 */
class Setting extends StaffApi
{


    /**
     * 获取团队成员
     */
    public function team()
    {
        $id = input('id');
        $customer = Customer::get($id);
        if (empty($customer)) {
            $this->error('客户不存在');
        }
        $owner_staff = Staff::where(['id' => $customer->owner_staff_id])->find();
        $rw_staffs = Staff::where(['id' => ['in', explode(',', $customer->rw_staff_id)],'status'=>1])->select();
        $ro_staffs = Staff::where(['id' => ['in', explode(',', $customer->ro_staff_id)],'status'=>1])->select();

        $staffs = [];
        if ($owner_staff) {
            $staffs[] = [
                'id' => $owner_staff->id,
                'name' => $owner_staff->name,
                'img' => $owner_staff->img,
                'post' => $owner_staff->post,
                'mobile' => $owner_staff->mobile,
                'roles' => 1,
                'is_edit' => 1,
            ];
        }
        foreach ($rw_staffs as $v) {
            $staffs[] = [
                'id' => $v->id,
                'name' => $v->name,
                'img' => $v->img,
                'post' => $v->post,
                'mobile' => $v->mobile,
                'roles' => 2,
                'is_edit' => 1,
            ];
        }
        foreach ($ro_staffs as $v) {
            $staffs[] = [
                'id' => $v->id,
                'name' => $v->name,
                'img' => $v->img,
                'post' => $v->post,
                'mobile' => $v->mobile,
                'roles' => 2,
                'is_edit' => 0,
            ];
        }

        $this->success('请求成功', $staffs);
    }

    /**
     * 修改团队成员
     */
    public function editShowStaff()
    {
        $id = input('id');
        $staff = input('staff/a');
        $model = Customer::get($id);
        if (empty($model)) {
            $this->error('客户不存在');
        }
        $ro_staff_id = [];
        $rw_staff_id = [];
        foreach ($staff as $v) {
            if ($v['is_edit'] == 1) {
                $rw_staff_id[] = $v['id'];
            } else {
                $ro_staff_id[] = $v['id'];
            }
        }
        $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
        $rw_staff_id=implode(',', $rw_staff_id);
        $ro_staff_id=implode(',', $ro_staff_id);
        $result = $model->save(['rw_staff_id' => ",{$rw_staff_id},",
            'ro_staff_id' => ",{$ro_staff_id},"]);
        if ($result === false) {
            $this->error('修改失败');
        }
        $this->success('修改成功');
    }

    //批量新增联合跟进人
    public function batchAddShowStaff()
    {
        $ids = input('ids');
        $staff_id = input('staff_id');
        $is_edit = input('is_edit', 0);
        if(empty($ids)){
            $this->error('参数不能为空');
        }
        $ids=explode(',',$ids);
        $customers = Customer::where(['id'=>['in',$ids]])->select();
        if (empty($customers)) {
            $this->error('客户不存在');
        }
        $staff_id = explode(',', $staff_id);
        $sname=Staff::where(['id'=>['in',$staff_id],'status'=>1])->column('name');
        $sname=implode(',',$sname);
        Db::startTrans();
        try {
            foreach ($customers as $customer){
                foreach ($staff_id as $sid) {
                    $rw_staff_id=explode(',',trim($customer['rw_staff_id'],','));
                    $ro_staff_id=explode(',',trim($customer['ro_staff_id'],','));
                    if ($is_edit == 1) {
                        if(in_array($sid,$rw_staff_id)){
                            continue;
                        }
                        $rw_staff_id[]=$sid;
                    } else {
                        if(in_array($sid,$ro_staff_id)){
                            continue;
                        }
                        $ro_staff_id[]=$sid;
                    }
                    OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'],
                        '批量新增联合跟进人：'.$sname);
                    Message::addMessage(Message::CUSTOMER_TYPE, $customer['id'], $sid, $this->auth->id, $this->auth->name . '邀请您联合跟进客户《' . $customer['name'] . '》');
                }
                $model=new Customer();
                $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
                $rw_staff_id=implode(',', $rw_staff_id);
                $ro_staff_id=implode(',', $ro_staff_id);
                $model->save(['rw_staff_id' => ",{$rw_staff_id},",
                    'ro_staff_id' => ",{$ro_staff_id},"],['id'=>$customer['id']]);
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }


        $this->success('添加成功');
    }
    //批量删除联合跟进人
    public function batchDelShowStaff()
    {
        $ids = input('ids');
        $staff_id = input('staff_id');
        $is_edit = input('is_edit', 0);
        if(empty($ids)){
            $this->error('参数不能为空');
        }
        $ids=explode(',',$ids);
        $customers = Customer::where(['id'=>['in',$ids]])->select();
        if (empty($customers)) {
            $this->error('客户不存在');
        }
        $staff_id = explode(',', $staff_id);
        $sname=Staff::where(['id'=>['in',$staff_id],'status'=>1])->column('name');
        $sname=implode(',',$sname);
        Db::startTrans();
        try {
            foreach ($customers as $customer){
                foreach ($staff_id as $sid) {
                    $rw_staff_id=explode(',',trim($customer['rw_staff_id'],','));
                    $ro_staff_id=explode(',',trim($customer['ro_staff_id'],','));
                    if ($is_edit == 1) {
                        if(in_array($sid,$rw_staff_id)){
                            continue;
                        }
                        $rw_staff_id = array_diff($rw_staff_id, [$sid]);
                    } else {
                        if(!in_array($sid,$ro_staff_id)){
                            continue;
                        }
                        $ro_staff_id = array_diff($ro_staff_id, [$sid]);
                    }
                    OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $customer['id'],
                        '批量新增联合跟进人：'.$sname);
                }
                $model=new Customer();
                $ro_staff_id = array_diff($ro_staff_id, $rw_staff_id);
                $rw_staff_id=implode(',', $rw_staff_id);
                $ro_staff_id=implode(',', $ro_staff_id);
                $model->save(['rw_staff_id' => ",{$rw_staff_id},",
                    'ro_staff_id' => ",{$ro_staff_id},"],['id'=>$customer['id']]);
            }
            Db::commit();
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }


        $this->success('删除成功');
    }
}